package com.pfs.internal.timestamp.reports;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;

import com.pfs.internal.timestamp.utils.Constant;

@SuppressWarnings("rawtypes")
public class PfsExcel<T extends FatherExcel> {
	private T rp;
	Map<String, Object> search;

	private String report_code;
	private String exported_name;
	private Workbook wb;

	public PfsExcel(Map<String, Object> search, T rp) {
		this.search = search;
		this.rp = rp;
	}

	public byte[] getReport() throws Exception {
		byte[] bytes = null;
		try {
			this.report_code = (String) search.get("report_code");
			if (!search.containsKey("reported_name") || StringUtils.isBlank((String) search.get("reported_name"))) {
				exported_name = report_code;
			} else {
				exported_name = (String) search.get("reported_name");
			}
			// create a workbook
			initWorkbook();
			// create a new report
			FatherExcel report = null;
			report = (FatherExcel) rp;
			report.setSearch(search);
			report.setWb(this.wb);
			report.layout();
			bytes = ReportUtil.wb2zip(wb, exported_name);
		} catch (Exception e) {
			e.printStackTrace();
			bytes = getReportNull();
		}
		return bytes;
	}

	public void initWorkbook() throws Exception {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			@SuppressWarnings("deprecation")
			String sRootPath = request.getRealPath("/");
			String file_name = sRootPath + "\\Reports\\" + this.report_code + ".xls";
			File file = new File(file_name);
			if (file.exists()) {
				FileInputStream fis = null;
				try {
					fis = new FileInputStream(file_name);
					wb = new HSSFWorkbook(fis);
				} catch (Exception e) {
					wb = new HSSFWorkbook();
				} finally {
					if (fis != null) {
						try {
							fis.close();
						} catch (Exception e) {
						}
					}
				}
			} else {
				wb = new HSSFWorkbook();
			}
		} catch (Exception e) {
			throw e;
		}
	}

	public void initFont() {

	}

	public void setExported_name(String name) {
		this.exported_name = name;
	}

	public byte[] getReportNull() throws Exception {
		byte[] bytes = null;
		try {
			wb = new HSSFWorkbook();
			Sheet sheet = wb.createSheet();
			HSSFRow rowData = (HSSFRow) sheet.createRow(0);
			HSSFCell cell = rowData.createCell(0);
			cell.setCellValue(new HSSFRichTextString("Xảy ra lỗi kết xuất excel"));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ZipOutputStream zos = new ZipOutputStream(baos);
			zos.setLevel(9);
			ZipEntry ze = new ZipEntry(report_code + ".xls");
			zos.putNextEntry(ze);
			wb.write(zos);
			zos.closeEntry();
			zos.close();
			bytes = baos.toByteArray();
			baos.close();
		} catch (Exception e) {
			throw e;
		}
		return bytes;
	}
}
